package com.meizi.experimentcode.dsl;

import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @author Administration
 * @date 2022/3/10 - 11:20 - 星期四
 * @project lambda
 * @package com.meizi.experimentcode.dsl
 * @by 晨
 */
public class MixedBuilder {

    public static order forCustomer(String customer,
                                    TradeBuilder... builders) {
        order order = new order();
        order.setCustomer(customer);
        Stream.of(builders).forEach( b -> order.addTrade(b.getTrade()));
        return order;
    }

    public static TradeBuilder buy (Consumer<TradeBuilder> consumer) {
        return buildTrade(consumer, Trade.Type.BUY);
    }

    public static TradeBuilder sell (Consumer<TradeBuilder> consumer) {
        return buildTrade(consumer, Trade.Type.SELL);
    }

    public static TradeBuilder buildTrade (Consumer<TradeBuilder> consumer,
                                           Trade.Type buy) {
        TradeBuilder builder = new TradeBuilder();
        builder.trade.setType(buy);
        consumer.accept(builder);
        return builder;
    }
}
